package com.bk.game;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.HttpAuthHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * 描述:
 * 
 * @author linqiang(866116)
 * @Since 2012-9-13
 */
public class WebActivity extends Activity {
	private WebView webview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		webview = new WebView(this);
		// 设置WebView属性，能够执行Javascript脚本
		webview.getSettings().setJavaScriptEnabled(true);
		// 加载需要显示的网页
		webview.loadUrl("http://tree-life.biz/android/");
		webview.setWebViewClient(new WebViewClient() {

			@Override
			public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
				super.doUpdateVisitedHistory(view, url, isReload);
				System.out.println("doUpdateVisitedHistory");
			}

			@Override
			public void onLoadResource(WebView view, String url) {
				super.onLoadResource(view, url);
				System.out.println("onLoadResource");
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);

			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				System.out.println("onPageStarted");
			}

			@Override
			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
				System.out.println("onReceivedError");
				super.onReceivedError(view, errorCode, description, failingUrl);
			}

			@Override
			public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
				super.onReceivedHttpAuthRequest(view, handler, host, realm);
				System.out.println("onReceivedHttpAuthRequest");
			}

			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// webview.getSettings().setJavaScriptEnabled(false);
				// openPage(url);
				view.loadUrl(url);
				return true;
			}

		});
		// 设置Web视图
		setContentView(webview);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	// private void openPage(String url) {
	// try {
	// Uri uri = Uri.parse(url);
	// Intent intent = new Intent(Intent.ACTION_VIEW, uri);
	// // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	// startActivity(intent);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
			webview.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
